#爬取豆瓣读书《平凡的世界》的1页短评信息，然后保存到文件中

import requests
from lxml import etree
url='https://book.douban.com/subject/1200840/comments/'
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
    }

#发出请求
response=requests.get(url,headers=headers)

html=etree.HTML(response.text)
#初始化保存短评的列表
comment_list=[]

comment=html.xpath('//*[@id="comments"]/div[1]/ul/li[1]/div[2]/p/span')
# print(comment)
for i in comment:
    result=i.xpath('./text()')
    # print(result)
    comment_list.append(''.join(result)) #将列表转化为字符串
print(comment_list)

with open('comment.txt','w',encoding='utf-8') as f:
    for i in comment_list:
        f.write(i+'\n')